package com.ers.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;

import com.ers.auth.HrAuthPassport;
import com.ers.model.Company;
import com.ers.model.HR;
import com.ers.model.Photo;
import com.ers.modelconvert.CompanyConvert;
import com.ers.service.CompanyService;
import com.ers.validmodel.CompanyModel;

@RequestMapping(value="/company")
public class CompanyController extends BaseController implements ServletContextAware{
	private CompanyService companyService;
	private ServletContext servletContext;
	private String savepath ;
	
	@HrAuthPassport
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register(Model m,HttpServletRequest request){
		HR hr = (HR)request.getSession().getAttribute("hr");
		if(hr.isIsvalid())
			return "redirect:/home/index";
		if(!m.containsAttribute("contentModel"))
			m.addAttribute("contentModel", new Company());
		return "company/register";
	}
	@HrAuthPassport
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(Model m,@Valid @ModelAttribute(value="companyModel")CompanyModel companyModel,BindingResult result,
			@RequestParam(value="hr_id")int id,HttpServletRequest request) throws IllegalStateException, IOException{		
		if(result.hasErrors())
			return register(m,request);		      				
        Company c = CompanyConvert.toCompany(companyModel);
		HR hr = new HR();
		hr.setId(id);
		c.setHr(hr);		
		companyService.add(c);	
		
		m.addAttribute("companyId", c.getId());
		
		HR hrv = (HR)request.getSession().getAttribute("hr");
		hrv.setIsvalid(true);
		hrv.setCompany(c);
		request.setAttribute("hr", hrv);
		return "company/uploadlicense";
	}
	
	@HrAuthPassport
	@RequestMapping(value="/modify",method=RequestMethod.GET)
	public String modify(Model m,HttpServletRequest request){
		HR hr = (HR) request.getSession().getAttribute("hr");		
		if(!m.containsAttribute("companyModel"))
			m.addAttribute("companyModel", hr.getCompany());
		return "company/modify";
	}
	@HrAuthPassport
	@RequestMapping(value="/modify",method=RequestMethod.POST)
	public String modify(Model m,@Valid @ModelAttribute("companyModel")CompanyModel companyModel,
			BindingResult result){
		companyService.update(CompanyConvert.toCompany(companyModel));
		return "company/modify";
	}
	@HrAuthPassport
	@RequestMapping(value="/show",method=RequestMethod.GET)
	public String show(Model m,HttpServletRequest request){
		HR hr = (HR) request.getSession().getAttribute("hr");	
		List<Photo> photos= companyService.loadPhoto(hr.getCompany().getId());
		m.addAttribute("photos", photos);
		return "company/show";
	}
	
	@HrAuthPassport
	@RequestMapping(value="/upload",method=RequestMethod.GET)
	public String upload(){
		return "company/uploadphoto";
	}
	
	
	
	
	
	public CompanyService getCompanyService() {
		return companyService;
	}
	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	public String getSavepath() {
		return savepath;
	}
	public void setSavepath(String savepath) {
		this.savepath = savepath;
	}
	
	
}
